from haystack.views import SearchView
from django.core.paginator import Page,Paginator
from .models import *
from django.http import Http404


class MySearchView(SearchView):
    def extra_context(self):  # 重载extra_context来添加额外的context内容
        context = super(MySearchView, self).extra_context()
        context['title'] = '搜索'
        context['isnew'] = 1



        # paginator = Paginator(self.results, self.results_per_page)
        # goodslist = paginator

        #
        # try:
        #     page_no = int(self.request.GET.get('page', 1))
        # except (TypeError, ValueError):
        #     raise Http404("Not a valid number for page.")
        #
        # if page_no < 1:
        #     raise Http404("Pages should be 1 or greater.")

        # start_offset = (page_no - 1) * self.results_per_page
        # self.results[start_offset:start_offset + self.results_per_page]
        #
        # paginator_copy = Paginator(self.results, self.results_per_page)
        # page_copy = paginator_copy.page(page_no)
        #
        #
        # try:
        #     page = paginator.page(page_no)
        # except InvalidPage:
        #     raise Http404("No such page!")

        # return (paginator, page)



        # pageindex = page.number
        # if goodslist.num_pages < 5:
        #     pagenumberlist = range(1, goodslist.num_pages + 1)
        # elif pageindex in [1, 2]:
        #     pagenumberlist = range(1, 6)
        # elif pageindex in [goodslist.num_pages - 1, goodslist.num_pages]:
        #     pagenumberlist = range(goodslist.num_pages - 4, goodslist.num_pages + 1)
        # else:
        #     pagenumberlist = range(pageindex - 2, pageindex + 3)
        #
        # context['pageindex'] = pageindex
        # context['goodslist'] = goodslist
        pagenumberlist = range(1,6)
        context['pagenumerlist'] = pagenumberlist


        return context

    # def get_query(self):
    #     queryset = super(MySearchView, self).get_query()
    #     # further filter queryset based on some set of criteria
    #     # return queryset.filter(pub_date__gte=date(2015, 1, 1))
    #     return queryset.order_by('-gclick')

